// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from "vue";
import App from "./App";
import router from "./router";
//导入 axios  库
import axios from "axios";
// 引用 ElementUI 插件
import ElementUI from "element-ui";
// 引用 ElementUI 样式
import "element-ui/lib/theme-chalk/index.css";
import store from "@/components/store";

// 通过 Vue.use 注册插件
Vue.use(ElementUI);

//  添加响应拦截器，401  的错误注销并跳转到登录页面
axios.interceptors.response.use((response) => {
  let data = response.data;
  console.log("main.js拦截器拦截的信息:", data);
  if (data.code == 401) {
    //  显示错误信息
    Vue.prototype.$message.error(data.msg);
    //  注销清除登录数据
    store.commit("logout");
    //  跳转到登录页面
    router.push({ path: "/login" });
    //  不需要响应了
    return;
  }
  return response;
});

//在Vue原型上绑定 axios  对象
Vue.prototype.$axios = axios;
Vue.prototype.$store = store;
Vue.config.productionTip = false;

/* eslint-disable no-new */
new Vue({
  el: "#app",
  router,
  components: { App },
  template: "<App/>",
});
